package com.guigu.www.denghao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.denghao.service.DHMemberService;
import com.guigu.www.mapper.MemberCaseHistoryMapper;
import com.guigu.www.mapper.MemberMapper;
import com.guigu.www.mapper.MemberTrendsMapper;
import com.guigu.www.pojo.Member;
import com.guigu.www.pojo.MemberCaseHistory;
import com.guigu.www.pojo.MemberTrends;
import com.guigu.www.pojo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Service
public class DHMemberServiceImpl implements DHMemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private MemberCaseHistoryMapper caseHistoryMapper;
    @Autowired
    private MemberTrendsMapper trendsMapper;

    @Override
    public PageVo<Member> initMember(Integer pageNum, Integer pageSize, String memName, String memPhone, String memAge) {
        IPage<Member> iPage = new Page<>(pageNum,pageSize);
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        if (!memAge.equals("")){
            wrapper.eq("mem_age",memAge);
        }
        if (!memName.equals("")){
            wrapper.like("mem_name",memName);
        }
        if (!memPhone.equals("")){
            wrapper.like("mem_phone",memPhone);
        }
        iPage = memberMapper.selectPage(iPage,wrapper);
        return new PageVo<>(iPage.getTotal(),iPage.getRecords());
    }

    @Override
    public void addMember(Member member, MemberTrends trends, MemberCaseHistory caseHistory) {
        // 获取当前时间
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format( now );
        member.setMemCreateDate(time);
        member.setMemState(0);
        member.setRoleId(3);
        // 从身份证上获取出生日期和年龄
        // 出生日期
        String birthday = member.getMemIdentity().substring(6, 10) + "-"
                + member.getMemIdentity().substring(10, 12) + "-"
                + member.getMemIdentity().substring(12, 14);
        //年龄
        int year = Calendar.getInstance().get(Calendar.YEAR);
        Integer age = (year - Integer.parseInt(member.getMemIdentity().substring(6, 10)));
        member.setMemBirthday(birthday);
        member.setMemAge(age);
        member.setMemPassword(member.getMemIdentity().substring(12)); // 默认密码

        // 添加会员基本信息表
        memberMapper.insert(member);
        //添加会员动态信息表
        trends.setMemId(member.getMemId());
        trendsMapper.insert(trends);
        //添加会员病历表
        caseHistory.setMemId(member.getMemId());
        caseHistoryMapper.insert(caseHistory);
    }

    @Override
    public void upMember(Member member, MemberTrends trends, MemberCaseHistory caseHistory) {
        // 从身份证上获取出生日期和年龄
        // 出生日期
        String birthday = member.getMemIdentity().substring(6, 10) + "-"
                + member.getMemIdentity().substring(10, 12) + "-"
                + member.getMemIdentity().substring(12, 14);
        //年龄
        int year = Calendar.getInstance().get(Calendar.YEAR);
        Integer age = (year - Integer.parseInt(member.getMemIdentity().substring(6, 10)));
        member.setMemBirthday(birthday);
        member.setMemAge(age);
        memberMapper.updateById(member);
        caseHistoryMapper.updateById(caseHistory);
        trendsMapper.updateById(trends);
    }

    @Override
    public Member queryByMemIdMember(Integer memId) {
        return memberMapper.selectOne(new QueryWrapper<Member>().eq("mem_id",memId));
    }

    @Override
    public MemberTrends queryByMemIdTrends(Integer memId) {
        return trendsMapper.selectOne(new QueryWrapper<MemberTrends>().eq("mem_id",memId));
    }

    @Override
    public MemberCaseHistory queryByMemIdCaseHistory(Integer memId) {
        return caseHistoryMapper.selectOne(new QueryWrapper<MemberCaseHistory>().eq("mem_id",memId));
    }

    @Override
    public Integer upMemState(Integer memId, Integer memState) {
        Member member = new Member();
        member.setMemId(memId);
        member.setMemState(memState);
        return memberMapper.updateById(member);
    }


}
